(function () {
    var util = {};
    //var completeUrl = "http://new.yytaomeng.com/";
    util.completeUrl = "http://192.168.1.11:81/";
    //路径拼接后半段列表
    var interfaceList = [
        {id: 'login', val: 'web/consult/selectbyrecommend'},
        {id:'register',val:''}
    ];
    //post请求方法
    util.ajax = function (key, data, success) {
        $.ajax({
            url: key.indexOf('http://') >= 0 ? key : util.interfaceUrl(key),
            data: data ? data : {},
            dataType: 'json',
            type: 'POST',
            jsonpCallback: "successCallback",
            async: true,
            success: success ? success : function () {
                }
        });
    };
    //get请求
    util.getAjax = function (key, data, success) {
        $.ajax({
            url: key.indexOf('http://') >= 0 ? key : util.interfaceUrl(key),
            data: data ? data : {},
            dataType: 'json',
            async: true,
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            jsonpCallback: "successCallback",
            success: function (data) {
                if (data.status == 1) {
                    success(data.content);
                } else {
                    console.log('请求错误');
                }
            },
            error: function (e) {
                console.log(e);
            }
        });
    };
    //获取接口名称
    util.getInterface = function (key) {
        var value = '';
        $.each(interfaceList, function (n, val) {
            if (val.id == key) {
                value = util.completeUrl + val.val;
                return false;
            }
        });
        return value;
    };

    //拼接接口路径
    util.interfaceUrl = function (url) {

       var completeUrls = url.indexOf('http://') >= 0 ? url : util.getInterface(url);
        return completeUrls;
    };

    //报错了
    util.error = function (data) {

    };
    // 判断是否为空
    util.isNull = function (str) {
        while (str.lastIndexOf(" ") >= 0) {
            str = str.replace(" ", "");
        }
        while (str.lastIndexOf("&nbsp;") >= 0) {
            str = str.replace("&nbsp;", "");
        }
        if (str.length == 0) {
            return 1;
        } else {
            return 0;
        }
    };
      util.phone=function(val){
        var test=/^1[34578]\d{9}$/;
        if(test.test(val)){
            return 1;
        }else{
            return 0;
        }
    };
    util.isNum=function(val){
        var test=/^[0-9]$/;
    };

    // 16进制编码转中文UTF-8 格式
  /*  function utf8to16(str) {
        var out, i, len, c;
        var char2, char3;
        out = "";
        len = str.length;
        i = 0;
        while (i < len) {
            c = str.charCodeAt(i++);
            switch (c >> 4) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    out += str.charAt(i - 1);
                    break;
                case 12:
                case 13:
                    char2 = str.charCodeAt(i++);
                    out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                    break;
                case 14:
                    char2 = str.charCodeAt(i++);
                    char3 = str.charCodeAt(i++);
                    out += String.fromCharCode(((c & 0x0F) << 12) |
                        ((char2 & 0x3F) << 6) |
                        ((char3 & 0x3F) << 0));
                    break;
            }
        }
        return out;
    }*/

    //获取路径传参
    //需要解析的问号传参，获取路径，调用接口
    /*   var url = location.search; //获取url中"?"符后的字串
     var a =tool.GetRequest(url);*/

/*    tool.GetRequest = function (url) {
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                var val = utf8to16(unescape(strs[i].split("=")[1]));

                theRequest[strs[i].split("=")[0]] = val;
            }
        }
        return theRequest;
    };*/


    //获取路径传参根据key查询参数值
    util.GetQueryString = function (key) {
        // key为url变量名称
        var urlGets = location.search.substr(1);
        if (urlGets.length > 0) {
            start = urlGets.indexOf(key + '=');
            if (start != -1) {
                start = start + key.length + 1;
                end = urlGets.indexOf('&', start);
                if (end == -1) {
                    end = urlGets.length
                }
                return decodeURIComponent(urlGets.substring(start, end));
            }
        }
      return "";
    };
    window.util = util;
})();